/**
 * @param {number} n
 * @param {number} k
 * @return {number[][]}
 */
var combine = function(n, k) {

  let result = [], tempArr = [];

  function dfs(depth, begin) {
    if(depth === k) {
      result.push([...tempArr]);
      return ;
    }
    for(let i = begin; i <= n; i++) {
      tempArr.push(i);
      dfs(depth + 1, i + 1);
      tempArr.splice(tempArr.length - 1, 1);
    }
  }

  dfs(0, 1);
  return result;
};

let n = 4, k = 2;
console.log(combine(n, k));
n = 1, k = 1;
console.log(combine(n, k));
